package com.xiaoxx.controller.sys;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xiaoxx.annotation.LogRecords;
import com.xiaoxx.common.error.ErrorException;
import com.xiaoxx.common.result.R;
import com.xiaoxx.common.result.RType;
import com.xiaoxx.model.dto.sys.SysRoleDto;
import com.xiaoxx.model.dto.sys.SysRoleQuery;
import com.xiaoxx.model.entity.sys.SysUser;
import com.xiaoxx.model.vo.sys.SysRoleAllListVo;
import com.xiaoxx.model.vo.sys.SysRoleVo;
import com.xiaoxx.service.SysRoleService;
import com.xiaoxx.service.SysUserService;
import com.xiaoxx.utils.JwtUtil;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * base--sys--角色管理 前端控制器
 * </p>
 *
 * @author HX
 * @since 2023-07-27
 */
@RestController
@RequestMapping("/api/sys/role")
@Api(value = "SysRoleController", tags = "角色管理")
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;

    @Resource
    private SysUserService sysUserService;

    @Autowired
    private HttpServletRequest request;

    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ApiOperation(value = "查询角色列表")
    @ApiImplicitParams({@ApiImplicitParam(name = "query", value = "查询角色列表", required = true, dataType = "SysRoleQuery", paramType = "Body")})
    @ApiResponses(value = {@ApiResponse(response = SysRoleVo.class, code = 200, message = "响应成功")})
    public R<IPage<SysRoleVo>> list(@RequestBody SysRoleQuery query) {
        return R.success(sysRoleService.listAdminRole(query));
    }


    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ApiOperation(value = "添加")
    @ApiImplicitParams({@ApiImplicitParam(name = "query", value = "查询角色列表", required = true, dataType = "SysRoleDto", paramType = "Body")})
    @ApiResponses(value = {@ApiResponse(response = String.class, code = 200, message = "响应成功")})
    @LogRecords(moduleName = "角色管理", content = "添加角色")
    public R<String> add(@RequestBody SysRoleDto dto) {
        if (StringUtils.hasLength(dto.getId())) {
            throw new ErrorException(RType.PARAM_ID_REQUIRED_FALSE);
        }
        return R.success(sysRoleService.add(dto));
    }

    @RequestMapping(value = "/allList", method = RequestMethod.GET)
    @ApiOperation(value = "查询所有角色列表")
    @ApiResponses(value = {@ApiResponse(response = SysRoleAllListVo.class, code = 200, message = "响应成功")})
    public R<List<SysRoleAllListVo>> list() {
        SysUser sysUser = sysUserService.getById(JwtUtil.getJwtUser(request).getUserId());
        if (sysUser == null) {
            throw new ErrorException(RType.USER_IS_DOES_NOT_EXIST);
        }
        return R.success(sysRoleService.listAllRole(sysUser));
    }

    @RequestMapping(value = "/findById", method = RequestMethod.GET)
    @ApiOperation(value = "通过id查询")
    @ApiImplicitParams({@ApiImplicitParam(name = "query", value = "通过id查询", required = true, dataType = "String", paramType = "query")})
    @ApiResponses(value = {@ApiResponse(response = SysRoleVo.class, code = 200, message = "响应成功")})
    public R<SysRoleVo> findById(@RequestParam String id) {
        return R.success(sysRoleService.findById(id));
    }

    @PutMapping(value = "/upd")
    @ApiOperation(value = "编辑")
    @ApiImplicitParams({@ApiImplicitParam(name = "dto", value = "编辑", required = true, dataType = "SysRoleDto", paramType = "Body")})
    @ApiResponses(value = {@ApiResponse(response = Boolean.class, code = 200, message = "响应成功")})
    @LogRecords(moduleName = "角色管理", content = "编辑角色信息")
    public R<Boolean> upd(@RequestBody SysRoleDto dto) {
        if (!StringUtils.hasLength(dto.getId())) {
            throw new ErrorException(RType.PARAM_ID_REQUIRED_TRUE);
        }
        return R.success(sysRoleService.upd(dto));
    }

    @RequestMapping(value = "/updDisable", method = RequestMethod.GET)
    @ApiOperation(value = "启用/禁用", notes = "启用/禁用")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query"),
            @ApiImplicitParam(name = "disable", value = "disable",required = true, paramType = "query")
    })
    @LogRecords(moduleName = "角色管理", content = "启用/禁用角色")
    public R<Boolean> updDisable(@RequestParam String id, @RequestParam Integer disable) {
        return R.success(sysRoleService.updDisable(id, disable));
    }

    @RequestMapping(value = "/del", method = RequestMethod.DELETE)
    @ApiOperation(value = "ID删除")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "删除", required = true, dataType = "String", paramType = "query")})
    @ApiResponses(value = {@ApiResponse(response = Boolean.class, code = 200, message = "响应成功")})
    @LogRecords(moduleName = "角色管理", content = "删除角色")
    public R<Boolean> del(@RequestParam String id) {
        return R.successDelete(sysRoleService.del(id));
    }
}

